#include "ui_bofugui.h"
#include "bofugui.h"


void BofuGui::on_pushButton_createUnitlist_clicked()
{
    logDate();
    copyFile(exe.absolutePath()+"/.data/example/unitlist.txt",dir.absolutePath()+"/unitlist.txt");
    ui->statusBar->showMessage(tr("desc-file created"),5000);
    writeToLog("echo \"unitlist-file created\" >> "+dir.absolutePath()+"/"+korpusname+".log");
    scriptDone();
}

void BofuGui::on_pushButton_editUnitlist_clicked()
{
    openURL(QUrl( ""+dir.absolutePath()+"/unitlist.txt"));
}

void BofuGui::on_pushButton_importUnitlist_clicked()
{
    QString sourceFile = QFileDialog::getOpenFileName(this, tr("Load unitlist file"), QDir::homePath());
    if (!sourceFile.isEmpty()){
        ui->lineEdit_unitlist->setText( sourceFile+" loaded!!!" );
        ui->statusBar->showMessage(tr("Unitlist loaded"),5000);
        QString destinationFile=dir.absolutePath()+"/unitlist.txt";
        copyFile(sourceFile,destinationFile);
        ui->codingButton->setEnabled(1);
        logDate();
        writeToLog("echo \"Unitlist loaded\" >> "+dir.absolutePath()+"/"+korpusname+".log");
        check_existing_files();

    }
}

void BofuGui::on_codingButton_clicked()
{
    if (!unitlist.exists()){
        errorFiles();
    }else {
        command = "cd "+dir.absolutePath()+"; perl "+bofu.absolutePath()+"/coding_scripts_for_lam/code.pl -ulist "+unitlist.fileName()+" -l "+ui->codeBox->text().toAscii().data()+" >> "+dir.absolutePath()+"/"+korpusname+".log 2>&1";
        commandexec = system(command.toAscii().data());
        ui->statusBar->showMessage(tr("Coded unitlist created"),5000);
        ui->pushButton_createDict->setEnabled(1);
        logDate();
        writeToLog("echo \"Coded unitlist created\" >> "+dir.absolutePath()+"/"+korpusname+".log");
        check_existing_files();
        scriptDone();
    }
}

void BofuGui::on_pushButton_createDict_clicked()
{
    if ((!filelist.exists())||(!transcriptionfile.exists())||(!convert_tables.exists())){
        errorFiles();
    }else {
        ui->statusBar->showMessage(tr("creating dict-file...please wait!!!"),5000);
        logDate();
        QString command;
        command = "cd "+dir.absolutePath()+"; perl "+bofu.absolutePath()+"/coding_scripts_for_lam/data_prepare.pl -sent transcription.txt -list filelist.txt -conv convert_tables.txt -wdir wav";
        if (ui->checkBox_2->isChecked()){
            command.append(" ­-wlist wordlist.txt");
        }
        if (ui->checkBox->isChecked()){
            command.append(" -slist sentencelist.txt");
        }
        command.append(" >> "+korpusname+".log 2>&1");
        commandexec = system(command.toAscii().data());

        QTime dieTime = QTime::currentTime().addSecs(10);
        while( QTime::currentTime() < dieTime )
                QCoreApplication::processEvents(QEventLoop::AllEvents, 100);


        command = "mv "+dir.absolutePath()+"/wav/*.txt "+dir.absolutePath()+"/txt/";
        commandexec = system(command.toAscii().data());

        command = "mv "+dir.absolutePath()+"/dict "+dir.absolutePath()+"/proto/";
        commandexec = system(command.toAscii().data());

        //creating the extra-file
        command = "cp "+dir.absolutePath()+"/proto/dict "+dir.absolutePath()+"/proto/extra >> "+dir.absolutePath()+"/"+korpusname+".log";
        commandexec = system(command.toAscii().data());

        ui->statusBar->showMessage(tr("dict-file created"),5000);
        writeToLog("echo \"dict-files created\" >> "+dir.absolutePath()+"/"+korpusname+".log");
        scriptDone();
        check_existing_files();
    }
}
